<template>
  <div class="reportFormPage" v-if="show">
    <div class="report-dom">
      <el-row>
        <el-col :span="5" :offset="19">
          <div class="pageNumBox">
            <span>第</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.page }}&nbsp;</span>
            <span>页，共</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.totalPage }}&nbsp;</span>
            <span>页</span>
          </div>
        </el-col>
      </el-row>
      <div class="topTwoBox">
        <div class="centerName">{{ this.form.baobmc }}</div>
        <div class="codeText">
          <tr v-for="(item, index) in form.baobbhList">
            {{
              item
            }}
          </tr>
        </div>
      </div>
      <div class="threeTopBox">
        <div class="left-border">
          <div class="box-title">检测单位名称：</div>
          <div class>{{ form.jiancdwmc }}</div>
        </div>
        <div class="right-border">
          <span>记录编号：</span>
          <span class="placeholder">{{ form.jilbh }}</span>
        </div>
      </div>
      <!-- .........................1111111111111111111111111111111......................................................... -->
      <div class="table-border">
        <table height="220px" class="top-table">
          <tbody>
            <tr>
              <td height="30" align="center" width="15%">
                <span style="width: 90px; display: inline-block">工程名称</span>
              </td>
              <td width="35%" class="text-center">
                <span>{{ form.gongcmc }}</span>
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">工程部位/用途</span>
              </td>
              <td width="35%" class="text-center">
                <span>{{ form.gongcbwyt }}</span>
              </td>
            </tr>
            <tr>
              <td height="50" align="center">样品信息</td>
              <td class="specimen-border" colspan="3">
                <span>样品名称：</span>
                <span :class="{ placeholder: !form.yangpmc }">{{ form.yangpmc }}</span>
                <span>；样品编号：</span>
                <span :class="{ 'placeholder-long': !form.yangpbh }">{{ form.yangpbh }}</span>
                <span>；样品数量：</span>
                <span :class="{ placeholder: !form.yangpsl }">{{ form.yangpsl }}</span>
                <span>；样品状态：</span>
                <span :class="{ placeholder: !form.yangpzt }">{{ form.yangpzt }}</span>
                <span>；来样时间：</span>
                <span :class="{ placeholder: !form.yangpsj }">{{ form.yangpsj }}</span>
              </td>
            </tr>
            <tr>
              <td height="25" align="center" width="15%">
                <span style="width: 90px; display: inline-block">试验检测日期</span>
              </td>
               <!-- <td width="35%" class="text-center">{{ form.shiyjcrq }}</td> -->                                            
 <td width="35%" class="text-center">
      <el-input v-model="form.shiyjcrq" />
</td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">试验条件</span>
              </td>
              <td v-if="isPdf" width="35%">
                温度：
                <span style="display: inline-block; width: 10%">{{ form.shiytjwd }}</span>
                ℃；湿度：
                <span style="display: inline-block; width: 10%">{{ form.shiytjsd }}</span
                >%RH
              </td>
              <td v-else width="35%">
                温度：
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjwd" class="height100" style="width: 15%" @blur="setGjbhcBgShiytj()" />℃；湿度：
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjsd" class="height100" style="width: 15%" @blur="setGjbhcBgShiytj()" />%RH
              </td>
            </tr>
            <tr>
              <td height="40" align="center" width="15%">
                <span style="width: 90px; display: inline-block">检测依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.jiancyj }}</span>
                <el-input v-else type="textarea" v-model="form.jiancyj" :rows="2" class="mark" @dblclick.native="showBsJiancyjView" readonly />
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">判定依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.pandyj }}</span>
                <el-input v-else type="textarea" v-model="form.pandyj" :rows="2" @dblclick.native="showBsJudgeBaseView" @focus="handleFocus('pandyj')" class="mark" />
              </td>
            </tr>
            <tr class="bottom-border">
              <td height="50" align="center">
                <div>主要仪器设备</div>
                <div>名称及编号</div>
              </td>
              <td colspan="3">
                <span v-if="isPdf">{{ form.zhuyyqsbmcjbh }}</span>
                <el-input v-else type="textarea" :rows="2" v-model="form.zhuyyqsbmcjbh" class="height100 width100 mark" @dblclick.native="showBsInstrumentView" readonly></el-input>
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .................................22222222222222222222222222222222............................................... -->
        <table height="610px" class="middle-table">
          <tbody>
            <tr>
              <td colspan="4">保护层厚度设计值（mm）</td>
              <td colspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" tabindex="200" v-model="form.baohcSjz" @blur="setGjbhcBgData()" @keydown.native="handleKeyDown($event, 0, 0)" :ref="'0_0'" />
              </td>
              <td colspan="3">钢筋间距设计值（mm）</td>
              <td colspan="2">
                <el-input :type="isPdf ? 'string' : 'number'" tabindex="200" v-model="form.gangjSjz" @blur="setGjbhcBgData()" @keydown.native="handleKeyDown($event, 0, 1)" :ref="'0_1'" />
              </td>
            </tr>
            <tr>
              <td colspan="4">保护层厚度允许偏差</td>
              <td colspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" tabindex="200" v-model="form.baohcYxpc" @blur="setGjbhcBgData()" @keydown.native="handleKeyDown($event, 1, 0)" :ref="'1_0'" />
              </td>
              <td colspan="3">钢筋间距允许偏差</td>
              <td colspan="2">
                <el-input :type="isPdf ? 'string' : 'number'" tabindex="200" v-model="form.gangjYxpc" @blur="setGjbhcBgData()" @keydown.native="handleKeyDown($event, 1, 1)" :ref="'1_1'" />
              </td>
            </tr>
            <tr>
              <td colspan="12">保护层厚度（mm）</td>
            </tr>
            <tr>
              <td colspan="2" width="10%">序号</td>
              <td>1</td>
              <td>2</td>
              <td>3</td>
              <td>4</td>
              <td>5</td>
              <td>6</td>
              <td>7</td>
              <td>8</td>
              <td>9</td>
              <td>10</td>
            </tr>
            <tr>
              <td colspan="2">实测值1</td>
              <td v-if="index < 10" v-for="(item, index) in form.baohcList" :key="index">
                <el-input :type="isPdf ? 'string' : 'number'" tabindex="200" v-model="item.shicz1" @blur="setGjbhcBgData()" @keydown.native="handleKeyDown($event, 2, index)" :ref="`2_${index}`" />
              </td>
            </tr>
            <tr>
              <td colspan="2">实测值2</td>
              <td v-if="index < 10" v-for="(item, index) in form.baohcList" :key="index">
                <el-input :type="isPdf ? 'string' : 'number'" tabindex="200" v-model="item.shicz2" @blur="setGjbhcBgData()" @keydown.native="handleKeyDown($event, 3, index)" :ref="`3_${index}`" />
              </td>
            </tr>
            <tr>
              <td colspan="2">序号</td>
              <td>11</td>
              <td>12</td>
              <td>13</td>
              <td>14</td>
              <td>15</td>
              <td>16</td>
              <td>17</td>
              <td>18</td>
              <td>19</td>
              <td>20</td>
            </tr>
            <tr>
              <td colspan="2">实测值1</td>
              <td v-if="index >= 10 && index < 20" v-for="(item, index) in form.baohcList" :key="index">
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.shicz1"
                  @blur="setGjbhcBgData()"
                  @keydown.native="handleKeyDown($event, 4, index - 10)"
                  :ref="`4_${index - 10}`"
                />
              </td>
            </tr>
            <tr>
              <td colspan="2">实测值2</td>
              <td v-if="index >= 10 && index < 20" v-for="(item, index) in form.baohcList" :key="index">
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.shicz2"
                  @blur="setGjbhcBgData()"
                  @keydown.native="handleKeyDown($event, 5, index - 10)"
                  :ref="`5_${index - 10}`"
                />
              </td>
            </tr>
            <tr>
              <td colspan="2">序号</td>
              <td>21</td>
              <td>22</td>
              <td>23</td>
              <td>24</td>
              <td>25</td>
              <td>26</td>
              <td>27</td>
              <td>28</td>
              <td>29</td>
              <td>30</td>
            </tr>
            <tr>
              <td colspan="2">实测值1</td>
              <td v-if="index >= 20 && index < 30" v-for="(item, index) in form.baohcList" :key="index">
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.shicz1"
                  @blur="setGjbhcBgData()"
                  @keydown.native="handleKeyDown($event, 6, index - 20)"
                  :ref="`6_${index - 20}`"
                />
              </td>
            </tr>
            <tr>
              <td colspan="2">实测值2</td>
              <td v-if="index >= 20 && index < 30" v-for="(item, index) in form.baohcList" :key="index">
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.shicz2"
                  @blur="setGjbhcBgData()"
                  @keydown.native="handleKeyDown($event, 7, index - 20)"
                  :ref="`7_${index - 20}`"
                />
              </td>
            </tr>
            <tr>
              <td colspan="2">序号</td>
              <td>31</td>
              <td>32</td>
              <td>33</td>
              <td>34</td>
              <td>35</td>
              <td>36</td>
              <td>37</td>
              <td>38</td>
              <td>39</td>
              <td>40</td>
            </tr>
            <tr>
              <td colspan="2">实测值1</td>
              <td v-if="index >= 30 && index < 40" v-for="(item, index) in form.baohcList" :key="index">
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.shicz1"
                  @blur="setGjbhcBgData()"
                  @keydown.native="handleKeyDown($event, 8, index - 30)"
                  :ref="`8_${index - 30}`"
                />
              </td>
            </tr>
            <tr>
              <td colspan="2">实测值2</td>
              <td v-if="index >= 30 && index < 40" v-for="(item, index) in form.baohcList" :key="index">
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.shicz2"
                  @blur="setGjbhcBgData()"
                  @keydown.native="handleKeyDown($event, 9, index - 30)"
                  :ref="`9_${index - 30}`"
                />
              </td>
            </tr>
            <tr>
              <td colspan="12">钢筋间距（mm）</td>
            </tr>
            <tr>
              <td colspan="2" width="10%">序号</td>
              <td>1</td>
              <td>2</td>
              <td>3</td>
              <td>4</td>
              <td>5</td>
              <td>6</td>
              <td>7</td>
              <td>8</td>
              <td>9</td>
              <td>10</td>
            </tr>
            <tr>
              <td colspan="2">实测值</td>
              <td v-if="index < 10" v-for="(item, index) in form.gangjList" :key="index">
                <el-input :type="isPdf ? 'string' : 'number'" tabindex="200" v-model="item.shicz1" @blur="setGjbhcBgData()" @keydown.native="handleKeyDown($event, 10, index)" :ref="`10_${index}`" />
              </td>
            </tr>

            <tr>
              <td colspan="2">序号</td>
              <td>11</td>
              <td>12</td>
              <td>13</td>
              <td>14</td>
              <td>15</td>
              <td>16</td>
              <td>17</td>
              <td>18</td>
              <td>19</td>
              <td>20</td>
            </tr>
            <tr>
              <td colspan="2">实测值</td>
              <td v-if="index >= 10 && index < 20" v-for="(item, index) in form.gangjList" :key="index">
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.shicz1"
                  @blur="setGjbhcBgData()"
                  @keydown.native="handleKeyDown($event, 11, index - 10)"
                  :ref="`11_${index - 10}`"
                />
              </td>
            </tr>
            <tr>
              <td colspan="2">序号</td>
              <td>21</td>
              <td>22</td>
              <td>23</td>
              <td>24</td>
              <td>25</td>
              <td>26</td>
              <td>27</td>
              <td>28</td>
              <td>29</td>
              <td>30</td>
            </tr>
            <tr>
              <td colspan="2">实测值</td>
              <td v-if="index >= 20 && index < 30" v-for="(item, index) in form.gangjList" :key="index">
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.shicz1"
                  @blur="setGjbhcBgData()"
                  @keydown.native="handleKeyDown($event, 12, index - 20)"
                  :ref="`12_${index - 20}`"
                />
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .....................................................33333333333333333................................................... -->
        <table height="60px" class="bottom-table">
          <tr>
            <td>
              <span v-if="isPdf" class="textarea-span">{{ form.fujsm }}</span>
              <el-input v-else type="textarea" :rows="2" v-model="form.fujsm" @focus="handleFocus('fujsm')" />
            </td>
          </tr>
        </table>
      </div>
      <!-- .................................44444444444............................................. -->
      <table height="22px" class="transparent">
        <tbody>
          <tr>
            <td width="10%" align="right">检测：</td>
            <td width="15%" align="left">
              <!-- {{this.form.jiancr}} -->
            </td>
            <td width="10%" align="right">记录：</td>
            <td width="15%" align="left">
              <!-- {{this.form.jilr}} -->
            </td>
            <td width="10%" align="right">复核：</td>
            <td align="left" width="15%">
              <!-- {{this.form.fuhr}} -->
            </td>
            <td width="10%">日期：</td>
            <td align="left" width="15%">
              <div style="display: inline-block; margin-left: 20px">年</div>
              <div style="display: inline-block; margin-left: 20px">月</div>
              <div style="display: inline-block ;margin-left: 20px">日</div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <bs-instrument-view ref="bsInstrumentViewRef" @select="selectInstrument"></bs-instrument-view>
    <bs-jiancyj-view ref="bsJiancyjViewRef" @select="selectJiancyj" :syType="syType"></bs-jiancyj-view>
    <bs-judge-base-view ref="bsJudgeBaseViewRef" @select="selectJudge" :syType="syType"></bs-judge-base-view>
  </div>
</template>

<script>
import decimal from "@/utils/big-decimal";
import Bus from "@/utils/bus";
import { handleFocus, emptyConvert, calculateMeanvalue, handleKeyDown } from "@/views/reportForms/utils";
import { getHntjgGjbhcjlbBsInfo } from "@/api/report_hntjg/hntjg";
import BsInstrumentView from "@/views/dept/bsInstrument/BsInstrumentView.vue";
import BsJiancyjView from "@/views/sycs/bsJiancyj/BsJiancyjView.vue";
import BsJudgeBaseView from "@/views/sycs/bsJudgeBase/BsJudgeBaseView.vue";
import BaQjDetailView from "@/views/sycs/baQjDetail/BaQjDetailView.vue";
import TableLineLogChart from "@/views/reportForms/components/TableLineLogChart.vue";

export default {
  name: "gjbhcjlb",
  components: {
    BsInstrumentView,
    BsJiancyjView,
    BsJudgeBaseView,
    BaQjDetailView,
    TableLineLogChart
  },
  props: {
    excelId: {
      type: [String, Number],
      default: () => ""
    },
    syType: {
      type: [String, Number],
      default: () => ""
    },
    page: {
      type: Number,
      default: () => 0
    },
    isPdf: {
      type: Boolean,
      default: () => false
    },
    printEmpty: {
      type: Boolean,
      default: () => false
    }
  },
  data() {
    return {
      form: {
        detailList: [],
        baobbhList: []
      },
      show: false
    };
  },

  watch: {
    isPdf(newVal, oldVal) {
      this.form = { ...emptyConvert(this.form, newVal) };
    }
  },
  created() {
    this.init();
  },
  methods: {
    // 根据报表id获取当前报表信息
    init() {
      this.show = false;
      getHntjgGjbhcjlbBsInfo(this.excelId || this.$defaultExcelId).then((response) => {
        this.form = response.data;
        this.setInitialValue();
        if (!this.form.baohcList || this.form.baohcList.length <= 0) {
          this.getDetailList("baohc");
        }
        if (!this.form.gangjList || this.form.gangjList.length <= 0) {
          this.getDetailList("gangj");
        }
        this.show = true;
        if (!this.form.fujsm) {
          this.form.fujsm = "附加声明：";
        }
      });
    },
    // 设置初始值
    setInitialValue() {
      if (this.form) {
        this.form.baobmc = this.form.baobmc || "钢筋位置及保护层厚度试验检测记录表";
        this.form.baobbh = this.form.baobbh || "JGLQ02030";
        this.$set(this.form, "baobbhList", []);
        this.form.baobbhList = ["JGLQ02030"];
        // this.form.baohcSjz = undefined;
        // this.form.gangjSjz = undefined;
      }
    },
    getDetailList(type) {
      let dataList = [];
      for (let i = 0; i < (type == "baohc" ? 40 : 30); i++) {
        const item = {
          detailId: undefined,
          excelId: this.excelId,
          sort: i + 1,
          shicz1: undefined,
          shicz2: undefined,
          type: type
        };
        dataList.push(item);
      }
      if (type == "baohc") {
        this.form.baohcList = dataList;
      } else {
        this.form.gangjList = dataList;
      }
    },
    // 显示检测依据列表
    showBsJiancyjView() {
      this.$refs.bsJiancyjViewRef.init(this.form.jiancyjIds);
    },
    // 检测依据选择
    selectJiancyj(info) {
      this.form.jiancyj = info.label;
      this.form.jiancyjIds = info.ids;
      Bus.$emit("setGjbhcBgJiancyj", { jiancyj: this.form.jiancyj, jiancyjIds: this.form.jiancyjIds });
    },
    // 显示判定依据列表
    showBsJudgeBaseView() {
      this.$refs.bsJudgeBaseViewRef.init(this.form.pandyjIds);
    },
    // 判断依据选择
    selectJudge(info) {
       this.form.pandyjIds = info.ids;
      this.form.pandyj = info.label;
    },
    // 显示仪器列表
    showBsInstrumentView() {
      this.$refs.bsInstrumentViewRef.init(this.form.shebIds);
    },
    // 仪器选择
    selectInstrument(info) {
      this.form.zhuyyqsbmcjbh = info.label;
      this.form.shebIds = info.ids;
      Bus.$emit("setGjbhcBgZhuyyqsbmcjbh", { zhuyyqsbmcjbh: this.form.zhuyyqsbmcjbh, shebIds: this.form.shebIds });
    },
    setGjbhcBgData() {
      // 钢筋间距
      let gangjSjz = this.form.gangjSjz;
      // 允许偏差
      let gangjYxpc = this.form.gangjYxpc || 0;
      let gangjSjzMin = Number(gangjSjz) - Number(gangjYxpc);
      let gangjSjzMax = Number(gangjSjz) + Number(gangjYxpc);
      let gangjScds = 0;
      let gangjHgds = 0;
      this.form.gangjList.forEach((item) => {
        let arr = [];
        if (item.shicz1) {
          gangjScds++;
          arr.push(item.shicz1);
        } else {
          item.shicz1 = null;
        }
        if (item.shicz2) {
          arr.push(item.shicz2);
        } else {
          item.shicz2 = null;
        }
        // 实测平均值
        let shicz = calculateMeanvalue(arr);
        if (shicz && gangjSjz && shicz >= gangjSjzMin && shicz <= gangjSjzMax) {
          gangjHgds++;
        }
      });
      // 保护层厚度
      let baohcSjz = this.form.baohcSjz;
      // 允许偏差
      let baohcYxpc = this.form.baohcYxpc || 0;
      let baohcSjzMin = Number(baohcSjz) - Number(baohcYxpc);
      let baohcSjzMax = Number(baohcSjz) + Number(baohcYxpc);
      let baohcScds = 0;
      let baohcHgds = 0;
      this.form.baohcList.forEach((item) => {
        if (item.shicz1) {
          baohcScds++;
        } else {
          item.shicz1 = null;
        }
        if (!item.shicz2) {
          item.shicz2 = null;
        }
        // 实测平均值
        let shicz = item.shicz1;
        if (shicz && baohcSjz && shicz >= baohcSjzMin && shicz <= baohcSjzMax) {
          baohcHgds++;
        }
      });

      const info = {
        gangjSjz: gangjSjz,
        gangjScds: gangjScds,
        gangjHgds: gangjHgds,
        baohcSjz: baohcSjz,
        baohcScds: baohcScds,
        baohcHgds: baohcHgds
      };
      Bus.$emit("setGjbhcBgData", info);
    },
    // 传值试验条件
    setGjbhcBgShiytj() {
      const info = {
        shiytjwd: this.form.shiytjwd,
        shiytjsd: this.form.shiytjsd
      };
      Bus.$emit("setGjbhcBgShiytj", info);
    },
    submitForm() {
      return this.form;
    },
    handleFocus(event) {
      handleFocus(event, this.page, this.form[event]);
    },
    handleKeyDown(event, rowIndex, colIndex) {
      const maxArr = [1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9];
      const ref = handleKeyDown(event, rowIndex, colIndex, maxArr);
      if (this.$refs[ref]?.length) {
        this.$refs[ref][0].focus();
      } else if (this.$refs[ref]) {
        this.$refs[ref].focus();
      }
    }
  }
};
</script>

<style scoped></style>
